package com.example.bookstore;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;

public class MyDataBaseHelper extends SQLiteOpenHelper {

    private Context context;

    //数据库中创建一张Book表
    public static final String Book = "create table Book (\n" +
            "id integer primary key autoincrement,\n" +
            "author text,\n" +
            "price real,\n" +
            "pages integer,\n" +
            "name text,\n" +
            "category_id integer)";

    public static final String Category ="create table Category (\n" +
            "id integer primary key autoincrement,\n" +
            "category_name text,\n" +
            "category_code integer)";

    //2.实现构造方法
    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        //int version-当前数据库的版本号，可用于对数据库进行升级操作
        super(context, name, factory, version);
        this.context = context;
    }



    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(Book);//执行建表语句，创建数据库
        Toast.makeText(context,"Create  Book succeeded", Toast.LENGTH_SHORT).show();
        sqLiteDatabase.execSQL(Category);//执行建表语句，创建数据库
        Toast.makeText(context,"Create  Category succeeded", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
